/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controladores;

import dominio.*;
import JPAControllers.*;
import JPAControllers.exceptions.*;
import Persistencia.*;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 *
 * @author Alith
 */
public class ControladorAutenticar {

    
    private login objLogin = new login();
    LoginJpaController controlador;    
    public String verificarUsuario(String usu, String pass){
        String result="";
         System.out.println("entre e verififcar");
         if(usu.equals("admin") && pass.equals("1234")){
            result="Administrador";
        }else{
            try {
                System.out.println("ANTES RESULT");
                controlador = new LoginJpaController();
                result=consultarLogin(usu, pass);
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
        }
        return result;
    }

    public void setLogin(login logIn) {
        this.objLogin = logIn;
    }

    public int verificarUsuarioId(String usu, String pass) {
         int result=-1;
         System.out.println("entre e verififcar");
         if(usu.equals("admin") && pass.equals("1234")){
            result=-2;
        }else{
            try {
                controlador = new LoginJpaController();
                result=consultarLoginId(usu, pass);
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
        }
         System.out.println(result+"   VERIFICAR USUARIO ID");
        return result;
    }

    public String consultarLogin(String log,String pass) throws NonexistentEntityException {
        String tipo=null;
        System.out.println("ANTES WAA");
        try {
            List<Login> a = controlador.findLoginEntities();
            String logg="",passs="";
            System.out.println(a+"  loginentities");
            for(int i=0;i<a.size();i++){
                logg=((Login)a.get(i)).getLogin();
                passs=((Login)a.get(i)).getContrasena();
                System.out.println(logg+"--"+passs+"--"+log+"--"+pass+"--    logg y pas");
                if(logg.equals(log)&&passs.equals(pass)){
                    if(((Fisioterapeuta)((Login)a.get(i)).getFisioterapeuta()).getEstado().equals("1")){
                        tipo=((Tipousuario)((Fisioterapeuta)((Login)a.get(i)).getFisioterapeuta()).getTipousuario()).getNombre();
                    }
                }
            }
        }catch(Exception e){}
        return tipo;
    }
   public int consultarLoginId(String log, String pass) {
        int id=-1;
        try {
            List<Login> a = controlador.findLoginEntities();
            String logg="",passs="";
            for(int i=0;i<a.size();i++){
                logg=((Login)a.get(i)).getLogin();
                passs=((Login)a.get(i)).getContrasena();
                if(logg.equals(log)&&passs.equals(pass)){
                    if(((Fisioterapeuta)((Login)a.get(i)).getFisioterapeuta()).getEstado().equals("1")){
                       id=((Fisioterapeuta)((Login)a.get(i)).getFisioterapeuta()).getIdFisioterapeuta();
                    }
                }
            }
        }catch(Exception e){}
        return id;
    }

    public Fisioterapeuta getFIsio(String idFisio){
        Fisioterapeuta retFis=null;
        ControladorFisioterapeuta objCon=new ControladorFisioterapeuta();
        retFis=objCon.encontrarFisioterapeuta(idFisio);
        return retFis;
    }
}
